# -*- coding: utf_8 -*-
# @Time : 2021/8/20 0020 10:01
# @Author : lyw
# @Remark : 定制接口Server
import time

from mxsoftpy import Server
import requests


class OenDataServer(Server):

    def __init__(self, host, sn):
        self.sn = sn
        self.host = host
        super().__init__()

    def att_log(self, door_id, info: str):
        """一体机出入记录处理"""
        pers_dict = {}
        # info = """
        # 1452 2015-07-30 15:16:28 0 1 0 0 0
        # 1452 2015-07-30 15:16:29 0 1 0 0 0
        # 1452 2015-07-30 15:16:30 0 1 0 0 0
        # 1452 2015-07-30 15:16:31 0 1 0 0 0
        # 1452 2015-07-30 15:16:33 0 1 0 0 0
        # 1452 2015-07-30 15:16:34 0 1 0 0 0
        # 1452 2015-07-30 15:16:35 0 1 0 0 0
        # 8965 2015-07-30 15:16:36 0 1 0 0 0
        # 8965 2015-07-30 15:16:37 0 1 0 0 0"""
        data = []
        count = 0
        for i in info.split("\n"):
            i = i.split(" ")
            # return "ok:%d" % i
            if pers_dict.get(i[0]):
                pers_id = pers_dict[i[0]]
            else:
                res = requests.get(
                    "http://%s/janitor/origin/personnel.sy?door_id=%s&serial_num=%s" % (self.host, door_id, i[0]))
                pers_id = res.json()["data"][0]
                if pers_id:
                    pers_dict[i[0]] = pers_id
                else:
                    continue
            create_time = time.mktime(time.strptime(" ".join(i[1:3]), "%Y-%m-%d %H:%M:%S"))
            data.append({"door_id": door_id, "pers_id": pers_id, "motion": i[3], "create_time": int(create_time)})
            count += 1
        requests.post("http://%s/janitor/public/add/record.sy" % self.host, json={"data": data})

        return count

    def oper_log(self, door_id, info):
        """一体机用操作信息"""
        # info = """USER
        # PIN=36234 Name=36234 Pri=0 Passwd= Card=133440 Grp=1 TZ=0001000000000000
        # USER
        # PIN=36235 Name=36235 Pri=0 Passwd= Card=133441 Grp=1 TZ=0001000000000000
        # """
        # info= "OPLOG 103\t14\t2015-07-30 10:22:34\t36234\t0\t0\t0"
        if info.startswith("USER"):
            data = []
            count = 0
            for i in info.split("USER\n"):
                if not i:
                    continue
                data_dict = {}
                for j in [j for j in i.split(" ") if j]:
                    if j.startswith("PIN"):
                        data_dict["serial_num"] = j.split("=")[1]
                        data_dict["badge_number"] = j.split("=")[1]
                    if j.startswith("Name"):
                        data_dict["name"] = j.split("=")[1]
                    if j.startswith("Card"):
                        data_dict["card_number"] = j.split("=")[1]
                data_dict["door_id"] = door_id
                data.append(data_dict)
                count += 1
            res = requests.post("http://%s/janitor/public/add/record/personnel.sy" % self.host, json={"data": data})
            return res.json()
        if info.startswith("OPLOG"):
            if info.split(" ")[1].split("\t")[0] == "103":
                serial_num = info.split(" ")[2].split("\t")[1]
                pers_data = requests.get(
                    "http://%s/janitor/origin/personnel.sy?door_id=%s&serial_num=%s" % (self.host, door_id, serial_num))
                pers = pers_data.json()["data"]
                if pers[0]:
                    pers[1]["is_del"] = 1
                    requests.post("http://%s/janitor/public/add/record/personnel.sy" % self.host,
                                  json={"data": [pers[1]]})
                    # return res.json()
                # return pers
        else:
            return "ok"
